<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        管理
        <small>列表</small>
    </h1>
</section>

<!-- Main content -->
<section class="content" id="admin-accounts-app">

    <div class="row">
        <div class="col-md-6">
            <a class="btn btn-default" @click="getList">刷新</a>
            <a v-show="!show.edit" class="btn btn-info" @click="add">添加</a>
            <a v-show="show.edit" class="btn btn-danger" @click="show.edit = false">关闭</a>
        </div>
    </div>

    <div class="row" v-show="!show.edit">
        <div class="col-md-12">
            <table class="table table-responsive table-striped table-bordered">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>账户</th>
                    <th>姓名</th>
                    <th>昵称</th>
                    <th>角色</th>
                    <th>用户手机号</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="item in list">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.realname}}</td>
                    <td>{{item.nickname}}</td>
                    <td>{{item.role_name}}</td>
                    <td>{{item.user_mobile}}</td>
                    <td>
                        <a class="btn btn-primary btn-xs" @click="edit(item)">编辑</a>

                        <a v-if="!item.admin_id" class="btn btn-info btn-xs" @click="setService(item)">设置客服</a>
                        <a v-if="item.admin_id" class="btn btn-warning btn-xs" @click="setService(item)">取消客服</a>

                        <a class="btn btn-danger btn-xs" @click="del(item)">删除</a>
                    </td>
                </tr>
                </tbody>
                <tfoot>
                <tr>
                    <td colspan="100">
                        <page :rows="page.rows" :last="page.last" v-model="page.page"></page>
                    </td>
                </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="row" v-show="show.edit">
        <div class="col-md-4 col-md-offset-2">
            <h3>编辑</h3>
            <div class="form" role="form">

                <div class="form-group" v-if="!edit_item.id">
                    <label>账户：</label>
                    <input class="form-control" v-model="edit_item.name">
                </div>

                <div class="form-group">
                    <label>姓名：</label>
                    <input class="form-control" v-model="edit_item.realname">
                </div>

                <div class="form-group">
                    <label>昵称：</label>
                    <input class="form-control" v-model="edit_item.nickname">
                </div>

                <div class="form-group">
                    <label>角色：<a class="btn btn-info btn-xs" @click="getRoles">刷新</a></label>
                    <select class="form-control" v-model="edit_item.role_id">
                        <option v-for="r in roles" :value="r.id">{{r.name}} - {{r.remark}}</option>
                    </select>
                </div>

                <div class="form-group">
                    <label>密码：</label>
                    <input class="form-control" type="password" v-model="edit_item.password">
                </div>

                <div class="form-group">
                    <label>设置用户手机号：<i style="color:#777">(此用户将拥有特权，可选)</i></label>
                    <input class="form-control" v-model="edit_item.user_mobile">
                </div>

                <div class="form-group">
                    <a class="btn btn-primary" @click="save">保存</a>
                    <a class="btn btn-default" @click="show.edit = false">关闭</a>
                </div>
            </div>
        </div>
    </div>

</section>
<!-- /.content -->

<script>


    var admin_accounts_app = new Vue({
        el: '#admin-accounts-app',
        data: {
            list: [],
            roles: [],
            edit_item: {},
            page: {
                last: 1,
                page: 1,
                rows: 10
            },
            classes: [],
            show: {
                edit: false
            }
        },
        created: function () {
            this.getRoles();
            this.getList();
        },
        methods: {
            getList: function () {
                ajax({
                    uri: '/admins/list',
                    data: this.page,
                    success: function(resp){
                        this.list = resp.data.data;
                        this.page.last = resp.data.last_page;
                    }.bind(this)
                });
            },
            getRoles: function () {
                ajax({
                    uri: '/admins/role_list_all',
                    data: {rows: 9999},
                    success: function (resp) {
                        this.roles = resp.data;
                    }.bind(this)
                })
            },
            add: function () {
                this.edit_item = {};
                this.show.edit = true;
            },
            edit: function (item) {
                this.edit_item = item;
                this.show.edit = true;
            },
            save: function () {
                var item = this.edit_item;
                if (!item.name || !item.realname) {
                    alert('请完善资料！');
                    return;
                }

                ajax({
                    uri: '/admins/save',
                    data: item,
                    success: function(resp){
                        this.show.edit = false;
                        this.getList();
                    }.bind(this)
                });
            },
            del: function (item) {
                ajax({
                    uri: '/admins/del',
                    data: {id: item.id},
                    success: function (resp) {
                        this.getList();
                    }.bind(this)
                })
            },
            setService: function (item) {
                var cancel = item.admin_id ? 1 : 0;
                ajax({
                    uri: '/admins/add_service',
                    data: {id:item.id, cancel: cancel},
                    success: function (resp) {
                        this.getList();
                    }.bind(this)
                })
            },
            turnPage: function (offset) {
                this.page.offset = offset;
            }
        },
        watch: {
            'page.page': function (nv, ov) {
                this.getList();
            },
            'page.offset': function (nv, ov) {
                this.getList();
            }
        }
    });
</script>